package proxydemo.dynamicproxy;

import sun.misc.ProxyGenerator;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Proxy;

/**
 * @author chaird
 * @create 2021-01-12 15:59
 */
public class DynamicApp {
  public static void main(String[] args) throws Exception{

    EmailService emailService = new EmailServiceImpl();

    EmailProxy emailProxy = new EmailProxy(emailService);

    EmailService proxyInstance =
        (EmailService)
            Proxy.newProxyInstance(
                    emailProxy.getClass().getClassLoader(),
                    emailService.getClass().getInterfaces(),
                    emailProxy);

    byte[] bytes = ProxyGenerator.generateProxyClass("proxy", emailService.getClass().getInterfaces());

    File file = new File("C:\\Users\\25682\\Desktop\\1\\proxy.class");
    FileOutputStream outputStream = new FileOutputStream(file);
    outputStream.write(bytes);

    proxyInstance.speak();
  }
}
